<script setup>
import { ref, watch } from 'vue'
import { userRegisterService, userLoginService } from '@/api/login'
import { useUserStore } from '@/stores/index'
import router from '@/router'
import { ElAlert, ElMessage } from 'element-plus'
import { h } from 'vue'
const isRegister = ref(false)
const ruleForm = ref({
  username: '',
  password: '',
  repassword: ''
})
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '用户名长度为2到5', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度为6到16', trigger: 'blur' }
  ],
  repassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度为6到16', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== ruleForm.value.password) {
          callback(new Error('两次密码不一致'))
        } else {
          callback()
        }
      }
    }
  ]
}
// 注册功能
// form表单接受验证
const form = ref()
const register = async () => {
  await form.value.validate()
  await userRegisterService(ruleForm.value)
  ElMessage({ message: '注册成功' })
  isRegister.value = false
}

// 监听切换时表单变换
watch(isRegister, () => {
  ruleForm.value = {
    username: '',
    password: '',
    repassword: ''
  }
})
const userStore = useUserStore()
// 登录功能
const login = async () => {
  await form.value.validate()
  const res = await userLoginService(ruleForm.value)
  console.log(res)
  userStore.setToken(res.data.data.token)
  router.push('/novel')
}
</script>
<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <!-- 注册 -->
      <el-form
        ref="form"
        size="large"
        autocomplete="off"
        v-if="isRegister"
        :model="ruleForm"
        :rules="rules"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            style="width: 240px"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            style="width: 240px"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="ruleForm.repassword"
            style="width: 240px"
            type="password"
            placeholder="请再次输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item class="flex">
          <el-button @click="register" type="primary">注册</el-button>
          <el-link type="info" @click="isRegister = false">返回</el-link>
        </el-form-item>
      </el-form>
      <!-- 登录 -->
      <el-form
        ref="form"
        size="large"
        autocomplete="off"
        v-else
        :model="ruleForm"
        :rules="rules"
      >
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            style="width: 240px"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            style="width: 240px"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-link type="info" @click="isRegister = true">去注册</el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<style>
.login-page {
  background-color: #fff;
  height: 100vh;
  .bg {
    background: url(../assets/爱人如养花！LGD你做到了！_9_鑫蓁诣祈安._来自小红书网页版.jpg)
      no-repeat center 10%;
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-around;
    }
  }
}
</style>
